package com.hnguigu.health.houduan.lcj.controller_lcj;

import com.hnguigu.health.houduan.lcj.pojo_lcj.*;
import com.hnguigu.health.houduan.lcj.service_lcj.TOrderService_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TOrdersettingService_lcj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/torder")
public class TOrderController_lcj {
    @Autowired(required = false)
    TOrderService_lcj ts;

    @Autowired(required = false)
    TOrdersettingService_lcj tosett;

    @PostMapping("/findAll")
    @ResponseBody
    public PagVo_lcj<TOrder_lcj> findAll(@RequestBody Map<String,String> map){
        return ts.findAll1(Integer.valueOf(map.get("page")),Integer.valueOf(map.get("num")),map.get("filenumber"));
    }

    //确认预约
    @GetMapping("/updMember")
    @ResponseBody
    public Integer updMember(String memberid, String orderupdatetime){
        TOrder_lcj tOrder_lcj = new TOrder_lcj();
        tOrder_lcj.setOrderupdatetime(orderupdatetime);
        tOrder_lcj.setMemberid(memberid);
        ts.updBymemberId(tOrder_lcj);
        return 1;
    }
    //取消预约
    @GetMapping("/updMeid")
    @ResponseBody
    public Integer updMeid(String memberid, String orderupdatetime){
        TOrder_lcj tOrder_lcj = new TOrder_lcj();
        tOrder_lcj.setOrderupdatetime(orderupdatetime);
        tOrder_lcj.setMemberid(memberid);
        ts.updBymeid(tOrder_lcj);
        return 1;
    }

    @PostMapping("/addthree")
    @ResponseBody
    public Integer addthree(@RequestBody Map<String,String> map) throws ParseException {
        TOrder_lcj tOrder_lcj = new TOrder_lcj();
        TMemberExamination_lcj tMemberExamination_lcj = new TMemberExamination_lcj();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dad = map.get("orderdate");
        Date parse = simpleDateFormat.parse(dad);

        String create = map.get("ordercreatetime");
        Date createtime = simpleDateFormat.parse(create);

        String filenum = map.get("filenumber");
        String a = map.get("setmealid");
        String[] split = a.split(",");

        TOrder_lcj tOrder_lcj1 = new TOrder_lcj();
        tOrder_lcj1.setOrderdate(map.get("orderdate"));
        tOrder_lcj1.setMemberid(filenum);
        List<TOrder_lcj> bysid = ts.getBysid(tOrder_lcj1);

        TOrder_lcj lastbean = ts.Lastbean();
        String code = lastbean.getToOrderno();
        String newTmid="";
        String substring="";
       if(bysid.size()!=0){
           return 4;
       }else {
           Integer byOrder = ts.getByOrder(map.get("orderdate"));
           TOrdersetting_lcj byOrder1 = tosett.getByOrder(map.get("orderdate"));
           if (byOrder1 == null) {
               return 3;
           } else {
               if (byOrder1.getNumber() <= byOrder) {
                   return 2;
               } else {
                   int i1 = byOrder1.getReservations() + 1;
                   byOrder1.setReservations(i1);
                   tosett.updateByPrimaryKeySelective(byOrder1);
                   for (int i = 0; i < split.length; i++) {
                       tOrder_lcj.setMemberid(filenum);
                       tOrder_lcj.setOrderdate(map.get("orderdate"));
                       tOrder_lcj.setOrdercreatetime(map.get("ordercreatetime"));
                       tOrder_lcj.setSetmealid(Integer.valueOf(split[i]));
                       //预约编号
                       if(lastbean==null){
                           newTmid="Y0001";
                       }else{
                           substring = code.substring(2, code.length());
                       }
                       if(Integer.parseInt(substring)<10){
                           newTmid= "Y000"+(Integer.parseInt(substring)+1);
                       }
                       if(Integer.parseInt(substring)>=10){
                           newTmid= "Y00"+(Integer.parseInt(substring)+1);
                       }
                       if(Integer.parseInt(substring)>=100){
                           newTmid= "Y0"+(Integer.parseInt(substring)+1);
                       }
                       tOrder_lcj.setToOrderno(newTmid);
                       ts.addtorder(tOrder_lcj);

                       //添加会员检查
                       tMemberExamination_lcj.setTSetmealId(Integer.valueOf(split[i]));
                       tMemberExamination_lcj.setTMemberId(filenum);
                       tMemberExamination_lcj.setExamDate(parse);
                       tMemberExamination_lcj.setCreatetime(createtime);
                       ts.addTE(tMemberExamination_lcj);
                   }
                   return 1;
               }
           }
       }
    }
    @GetMapping("/getByordate")
    @ResponseBody
    public Integer getByordate(String orderdate){
        Integer byOrder = ts.getByOrder(orderdate);
        return byOrder;
    }

}
